<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class DocList extends Model
{
    public $dateFormat = 'U';

    /** 排序结构 */
    public static function plumb($model,$pid=0,$prefix='--')
    {
        $data = array();
        foreach($model as $item){
            if($item['pid']==$pid){
                $item['name'] = $prefix.$item['name'];
                $data[] = $item;
                $child = self::plumb($model,$item['id'],$prefix.$prefix);
                $data = array_merge($data,$child);
            }
        }

        return $data;
    }

    /** 首个子目录 */
    public static function firstChild($version_id = '', $list_id = '')
    {
        if (!empty($list_id)) return $list_id;
        $id = self::where(compact('version_id'))->where('pid','!=',0)->orderBy('sort', 'asc')->value('id');
        return $id;
    }

    /** 关联detail */
    public function docDetail()
    {
        return $this->hasOne(DocDetail::class,'list_id','id');
    }

}
